package edu.rit.hvz.android;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Radar extends MapActivity implements LocationListener {
	// The funky math is used to convert the longitude and latitude to
	// microdegrees
	private final static GeoPoint RIT_LOCATION = new GeoPoint((int) (43.084154 * 1e6), (int) (-77.674627 * 1e6));
	
	private LinearLayout linearLayout;
	private MapView mapView;
	private MapController mc;
	private List<Overlay> mapOverlays;
	private Drawable drawable;
	private HVZItemizedOverlay itemizedoverlay;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.onActivityCreateSetTheme(this);
		setContentView(R.layout.radar_layout);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mc = mapView.getController();
		mapOverlays = mapView.getOverlays();
		drawable = new MapPlayerIcon();
		itemizedoverlay = new HVZItemizedOverlay(drawable, this);

		mc.setCenter(RIT_LOCATION);
		mc.setZoom(16);
		mapView.setSatellite(true); // Tells the map to display satellite images

		LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// Activate this line to receive location updates
		//lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
		//		this);
		
		Drawable playerIcon = new MapPlayerIcon(Color.parseColor("#2AAAFF"), 12);
		GeoPoint playerLoc = new GeoPoint((int) (43.084436 * 1e6), (int) (-77.679654 * 1e6));
		OverlayItem playerOverlayItem = new OverlayItem(playerLoc, "Your Location", "");
		HVZItemizedOverlay playerLocOverlay = new HVZItemizedOverlay(playerIcon, this);
		playerLocOverlay.addOverlay(playerOverlayItem);
		mapOverlays.add(playerLocOverlay);
		
		Drawable squadIcon = new MapPlayerIcon(Color.parseColor("#FF5555"), 12);
		GeoPoint squadLoc = new GeoPoint((int) (43.084203 * 1e6), (int) (-77.677127 * 1e6));
		OverlayItem squadOverlayItem = new OverlayItem(squadLoc, "Squad Member Location", "");
		HVZItemizedOverlay squadLocOverlay = new HVZItemizedOverlay(squadIcon, this);
		squadLocOverlay.addOverlay(squadOverlayItem);
		
		squadLoc = new GeoPoint((int) (43.083907 * 1e6), (int) (-77.679324 * 1e6));
		squadOverlayItem = new OverlayItem(squadLoc, "Squad Member Location", "");
		squadLocOverlay.addOverlay(squadOverlayItem);
		
		squadLoc = new GeoPoint((int) (43.084804 * 1e6), (int) (-77.667686 * 1e6));
		squadOverlayItem = new OverlayItem(squadLoc, "Squad Member Location", "");
		squadLocOverlay.addOverlay(squadOverlayItem);		
		
		mapOverlays.add(squadLocOverlay);
		
		

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLocationChanged(Location location) {
		if (location != null) {
			double lat = location.getLatitude();
			double lng = location.getLongitude();
			GeoPoint p = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
			OverlayItem overlayitem = new OverlayItem(p, "Current Location",
					"" + lat + " " + lng);
			drawable = new MapPlayerIcon();
			itemizedoverlay = new HVZItemizedOverlay(drawable, this);
			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.clear();
			mapOverlays.add(itemizedoverlay);
			mapView.refreshDrawableState();
			mc.setCenter(p);
		}
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {

	}
}
